package com.xwh.shop.goods.service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xwh.shop.common.PageResult;
import com.xwh.shop.common.Result;
import com.xwh.shop.goods.model.entity.Goods;
import com.xwh.shop.goods.model.vo.GoodsDetailsVO;
import com.xwh.shop.goods.model.vo.GoodsPageVO;
import com.xwh.shop.goods.service.service.GoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xwh
 * @since 2025-07-16
 */
@Slf4j
@RestController
@RequestMapping("/goods")
@Tag(name = "商品管理")
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    @PostMapping("")
    public Result<Object> add(@RequestBody Goods goods){
        log.info("添加商品 goods:{}",goods);
        goodsService.save(goods);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<Goods> findById(@Parameter(description = "商品ID") @PathVariable Long id){
        log.info("通过ID查询商品 id:{}",id);
        Goods goods = goodsService.getById(id);
        log.info("找到的商品 goods:{}",goods);
        return Result.success(goods);
    }

    @GetMapping("findDetailsById/{id}")
    public Result<GoodsDetailsVO> findDetailsById(@PathVariable Long id){
        GoodsDetailsVO goodsDetailsVO = goodsService.findDetailsById(id);
        log.info("通过ID查询商品详情 goodsDetailVO:{}",goodsDetailsVO);
        return Result.success(goodsDetailsVO);
    }
    // 小程序是？传参 用@RequestParam接收
    @GetMapping("/page")
    public PageResult<GoodsPageVO> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String name,/*非必须*/
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) Byte status){
        IPage<GoodsPageVO> goodsPageVOIPage = goodsService.pageQuery(current, pageSize, name, categoryId, status);
        log.info("goodsPageVOIPage:{}",goodsPageVOIPage);
        return PageResult.success(goodsPageVOIPage);
    }


    @Operation(summary = "更新商品", description = "更新已有的商品信息")
    @ApiResponse(responseCode = "200", description = "更新成功")
    @PutMapping("")
    public Result<Boolean> update(@RequestBody Goods goods) {
        log.info("更新商品：{}", goods);
        boolean result = goodsService.updateById(goods);
        return Result.success(result);
    }

    @Operation(summary = "删除商品", description = "根据ID删除商品")
    @ApiResponse(responseCode = "200", description = "删除成功")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@Parameter(description = "商品ID") @PathVariable Long id) {
        log.info("删除商品：{}", id);
        boolean result = goodsService.removeById(id);
        return Result.success(result);
    }
}
